
#import "PrefsController.h"
#import "PrefsPanel.h"
#import "scsi_cd.subproj/ListScsi.h"

@implementation PrefsController

- showPrefsPanel:sender
{
 /*
  * mCD_Prefs.nib is setup with this object as it's owner, so it fills in the
  * ID for prefsPanel 
  */
    if (!prefsPanel)
	[NXApp loadNibSection:"mCD_Prefs.nib" owner:self];

 /* ListScsi seems to cause trouble... */
 /* so skip: if ( ! list_SCSI ) list_SCSI = [[ListScsi alloc] init]; */

    [prefsPanel showPrefsPanel:self usingScsiList:list_SCSI];

    return self;
}

#define Def_CDDRIVE "CDDrive"
- _findFirstCdDrive
{
    int                 devIndex;

    if (!list_SCSI)
	list_SCSI = [[ListScsi alloc] init];

    globRawDeviceName[0] = '\0';
    for (devIndex = 0; devIndex <= 6; devIndex++) {
	if ([list_SCSI getTypeOfDev:devIndex] == DEVTYPE_CDROM) {
	    sprintf(globRawDeviceName, "/dev/rsd%dh", devIndex);
	    break;
	}
    } /* end for(devIndex =... */

    if (globRawDeviceName[0] == '\0') {
	NXRunAlertPanel(0,
	  "Can't seem to find CD-ROM drive,\nassuming /dev/rsd1h will work",
			0, 0, 0);
	strcpy(globRawDeviceName, "/dev/rsd1h");
    }
    return self;
}

- _dreadRawDeviceName
{
    strcpy(globRawDeviceName, "/dev/rsd1h");
    if (NXGetDefaultValue([NXApp appName], Def_CDDRIVE)) {
	strcpy(globRawDeviceName, NXGetDefaultValue([NXApp appName],
						    Def_CDDRIVE));
    }
    if (!strcmp(globRawDeviceName, "/dev/rsd0h"))
	globRawDevicePrefTag = 0;
    if (!strcmp(globRawDeviceName, "/dev/rsd1h"))
	globRawDevicePrefTag = 1;
    if (!strcmp(globRawDeviceName, "/dev/rsd2h"))
	globRawDevicePrefTag = 2;
    if (!strcmp(globRawDeviceName, "/dev/rsd3h"))
	globRawDevicePrefTag = 3;
    if (!strcmp(globRawDeviceName, "/dev/rsd4h"))
	globRawDevicePrefTag = 4;
    if (!strcmp(globRawDeviceName, "/dev/rsd5h"))
	globRawDevicePrefTag = 5;
    if (!strcmp(globRawDeviceName, "/dev/rsd6h"))
	globRawDevicePrefTag = 6;

 /* the more complicated option */
    if (!strcmp(globRawDeviceName, "FirstDriveFound")) {
	globRawDevicePrefTag = USE_FIRST_CDDRIVE_TAG;
	[self _findFirstCdDrive];
    }
    return self;
}

- setRawDevicePrefTag:(int)prefTag andSave:(BOOL)dwriteIt
{
    globRawDevicePrefTag = prefTag;

    if (globRawDevicePrefTag == USE_FIRST_CDDRIVE_TAG)
	[self _findFirstCdDrive];
    else if ((prefTag >= 0) && (prefTag <= 6))
	sprintf(globRawDeviceName, "/dev/rsd%dh", prefTag);

    if (dwriteIt) {
	if (globRawDevicePrefTag == USE_FIRST_CDDRIVE_TAG)
	    NXWriteDefault([NXApp appName], Def_CDDRIVE, "FirstDriveFound");
	else
	    NXWriteDefault([NXApp appName], Def_CDDRIVE, globRawDeviceName);
    }
    return self;
}

- (char *)rawDeviceName
{
    return globRawDeviceName;
}

- (int)rawDevicePrefTag
{
    return globRawDevicePrefTag;
}

#define Def_SEPVOL "SeparateVolumes"
- _dreadSeparateVolumes
{
    globSeparateVolumes = NO;
    if (NXGetDefaultValue([NXApp appName], Def_SEPVOL)) {
	strcpy(tempStr, NXGetDefaultValue([NXApp appName],
					  Def_SEPVOL));
	if (!strcmp(tempStr, "yes"))
	    globSeparateVolumes = YES;
	if (!strcmp(tempStr, "Yes"))
	    globSeparateVolumes = YES;
	if (!strcmp(tempStr, "YES"))
	    globSeparateVolumes = YES;
    }
    return self;
}

- setSeparateVolumes:(BOOL)onOffSetting andSave:(BOOL)dwriteIt
{
    globSeparateVolumes = onOffSetting;
    if (dwriteIt) {
	if (globSeparateVolumes)
	    NXWriteDefault([NXApp appName], Def_SEPVOL, "YES");
	else
	    NXRemoveDefault([NXApp appName], Def_SEPVOL);
    }
    return self;
}

- (BOOL)separateVolumes
{
    return globSeparateVolumes;
}

#define Def_CONSDEB "ConsoleDebugMsgs"
- _dreadConsoleDebugMsgs
{
    globConsoleDebugMsgs = NO;
    if (NXGetDefaultValue([NXApp appName], Def_CONSDEB)) {
	strcpy(tempStr, NXGetDefaultValue([NXApp appName],
					  Def_CONSDEB));
	if (!strcmp(tempStr, "yes"))
	    globConsoleDebugMsgs = YES;
	if (!strcmp(tempStr, "Yes"))
	    globConsoleDebugMsgs = YES;
	if (!strcmp(tempStr, "YES"))
	    globConsoleDebugMsgs = YES;
    }
    return self;
}

- setConsoleDebugMsgs:(BOOL)onOffSetting andSave:(BOOL)dwriteIt
{
    globConsoleDebugMsgs = onOffSetting;
    if (dwriteIt) {
	if (globSeparateVolumes)
	    NXWriteDefault([NXApp appName], Def_CONSDEB, "YES");
	else
	    NXRemoveDefault([NXApp appName], Def_CONSDEB);
    }
    return self;
}

- (BOOL)consoleDebugMsgs
{
    return globConsoleDebugMsgs;
}

#define DefPfx_CONTROLFRAME "ControlFrame"	/* prefix of controlFrameName */
- _dreadControlFrame
{
    if (NXGetDefaultValue([NXApp appName], controlFrameName)) {
	strcpy(tempStr, NXGetDefaultValue([NXApp appName],
					  controlFrameName));
	[mainPanel_ID setFrameFromString:tempStr];
    }
    return self;
}

- saveControlFrame:(BOOL)wantSave
{
    if (wantSave) {
	[mainPanel_ID saveFrameToString:&tempStr[0]];
	NXWriteDefault([NXApp appName], controlFrameName, tempStr);
    } else
	NXRemoveDefault([NXApp appName], controlFrameName);

    return self;
}

#define Def_VOLUMES "Volumes"
#define DEF_VOLUMESETTING 220
- _dreadVolumes
{
    char               *start, *next;

    globLeftVolume = DEF_VOLUMESETTING;
    globRightVolume = DEF_VOLUMESETTING;
    if (NXGetDefaultValue([NXApp appName], Def_VOLUMES)) {
	strcpy(tempStr, NXGetDefaultValue([NXApp appName],
					  Def_VOLUMES));
	start = tempStr;
	if (*start == ' ')
	    start++;
	globLeftVolume = globRightVolume = strtod(start, &next);
	if (start != next) {
	    start = next;
	    if (*start == ' ')
		start++;
	    if (*start != '\0')
		globRightVolume = strtod(start, &next);
	}
	globLeftVolume = MIN(globLeftVolume, 255);
	globRightVolume = MIN(globRightVolume, 255);
	if (!globSeparateVolumes)
	    globLeftVolume = globRightVolume = MIN(globLeftVolume, globRightVolume);
    }
    return self;
}

- saveVolumes:(BOOL)wantSave
{
    int                 leftVolume, rightVolume;

    if (wantSave) {
	[cdInfo_ID getVolumes:&leftVolume:&rightVolume];
	if (!globSeparateVolumes)
	    leftVolume = rightVolume = MIN(leftVolume, rightVolume);
	if (leftVolume == rightVolume)
	    sprintf(tempStr, "%d", leftVolume);
	else
	    sprintf(tempStr, "%d %d", leftVolume, rightVolume);
	NXWriteDefault([NXApp appName], Def_VOLUMES, tempStr);
    } else
	NXRemoveDefault([NXApp appName], Def_VOLUMES);

    return self;
}

- getVolumes:(int *)leftVolPtr :(int *)rightVolPtr
{
    *leftVolPtr = globLeftVolume;
    *rightVolPtr = globRightVolume;
    return self;
}

/* this has to be the last action defined, as it references
   many methods which are defined internal to this module */
- initGlobalPreferences:cdInfo mainPanel:ctrlPanel
{
    NXDefaultsVector    defs =
    {
     {DefPfx_CONTROLFRAME, NULL},	/* must be 0-th element */
     {Def_CDDRIVE, NULL},	/* should key on hostname */
     {Def_SEPVOL, NULL},
     {Def_CONSDEB, NULL},
     {Def_VOLUMES, NULL},	/* should key on cd-rom model */
     {NULL, NULL}
    };

    cdInfo_ID = cdInfo;
    mainPanel_ID = ctrlPanel;

 /* build some defaults names based on current machine/config */
    [NXApp getScreenSize:&mainScreenSize];
    sprintf(controlFrameName, "%s-%1.0fx%1.0f", DefPfx_CONTROLFRAME,
	    mainScreenSize.width, mainScreenSize.height);
    defs[0].name = controlFrameName;	/* replace 0-th element */

    NXRegisterDefaults([NXApp appName], defs);

    [self _dreadRawDeviceName];
    [self _dreadSeparateVolumes];	/* must do before _dreadVolumes */
    [self _dreadConsoleDebugMsgs];
    [self _dreadControlFrame];	/* sets location of cntrlPanel */
    [self _dreadVolumes];

    return self;
}


@end
